﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using LiftHub.Controls;
using LiftHub.Data;
using LiftHub.Services;
using LiftHub.ViewModels.Pages;
using LiftHub.Views;

namespace LiftHub.Pages;

public partial class NotesPage : UserControl
{
    public NotesPage()
    {
        InitializeComponent();
    }

    private void NoteDeleteMenuClick(object? sender, RoutedEventArgs e)
    {
        if(sender is not Control control)return;
        if(control.DataContext is not Note note)return;
        if(DataContext is not NotesPageModel model)return;
        model.Remove(note);
    }

    private void NoteItemEditMenuClick(object? sender, RoutedEventArgs e)
    {
        if(sender is not Control control)return;
        if(control.DataContext is not Note note)return;
        Edit(note);
    }

    private void Edit(Note? note,bool isNew = false)
    {
        var main = ViewService.GetView<MainView>(this);
        if(main == null)return;
        main.Navigate(new NoteItemControl(note,isNew){DataContext = DataContext});
    }

    private void NoteItemTapped(object? sender, TappedEventArgs e)
    {
        if(sender is not Control control)return;
        if(control.DataContext is not Note note)return;
        Edit(note);
    }

    private void CreateClick(object? sender, RoutedEventArgs e)
    {
        Edit(null,true);
    }
}